package com.situ.ymall.controller;

import com.situ.ymall.entity.Address;
import com.situ.ymall.entity.Order;
import com.situ.ymall.entity.User;
import com.situ.ymall.mapper.OrderMapper;
import com.situ.ymall.quartz.QuartManager;
import com.situ.ymall.service.AddressService;
import com.situ.ymall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private QuartManager quartManager;
    /**
     * 添加订单
     */
    @PostMapping("/add")
    public String add(@RequestParam("cartId") Integer[] cartIds, HttpSession session, Model model){
        // 当前用户
        // 取出用户的ID
        User user = (User) session.getAttribute("user");
        //调用service
        try {
            Order order = orderService.add(user.getId(), cartIds);
            //启动job，让job在30分钟后执行，
            quartManager.addJob(order.getId());
            /*
            Order{id='4fd6beec-fc9b-4572-94ef-7d2928e2cddc', userId=1, addressId=null, time=Tue Oct 26 15:25:41 CST 2021, express='null', status=0,
                orderDetails=[OrderDetails{id=42, goodsId=25, orderId='4fd6beec-fc9b-4572-94ef-7d2928e2cddc', price=6799.00, count=1,
                        goods=Goods{id=25, goodsSortId=7, name='ipone13', dscp='轻薄潮流设计 | 丝绒AG工艺 | 原生美肌人像 | 像素级肌肤焕新技术 | 3200万高清质感自拍 | 双柔光灯+自动对焦 | 3D曲面OLED柔性屏 | 120Hz+Dolby Vision | 4500mAh 大电量 | 55W有线闪充 | 立体声双扬声器 | 多功能NFC', price=6799.00, version='8GB+128GB', color='闪闪黑', suit='标准版', count=13, status=0, goodsSort=null, pictures=[com.situ.ymall.entity.GoodsPicture@4d8f27b1]}
                }]}
             */
            model.addAttribute("order",order);

            // 查询用户的地址信息
            List addresses = addressService.getByUerId(user.getId());
            model.addAttribute("addresss",addresses);

            //订单生成成功
           return "user/order";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("error",e.getMessage());
            //跳转到一个错误页面
            return "user/error";
        }
    }

    /*
    支付页面
     */
    @GetMapping("/pay")
    public String pay(String orderId, Integer addressId ,Model model){

        //根据订单ID，查询订单信息
        Order order = orderService.getById(orderId);
        // 利用model传递到pay页面中
        model.addAttribute("order",order);
        // 根据addressID查询地址信息
        Address address = addressService.getById(addressId);
        //再利用model传递到pay页面中
        model.addAttribute("address",address);
        //将收货地址写入数据库
        order.setAddressId(addressId);
        int result = orderService.update(order);
        return "user/pay";
    }

    @PostMapping("/pay")
    public String pat(String id,String paypwd,HttpSession session,Model model){
        User user = (User) session.getAttribute("user");

        try {
            int result = orderService.pay(id,paypwd,user.getId());
            //支付成功
            // 跳转页面 - > 订单列表
            return "redirect:/user/order/list";
        } catch (Exception e) {
            e.printStackTrace();
            // 支付失败
            model.addAttribute("error",e.getMessage());
            return "user/error";
        }
    }

    @GetMapping("/list")
    public String list(){
        return "user/orderList";
    }

}
